package com.kcsj.util;
//201902104058胡庆阳
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by STGLJY@126.COM on 2017/12/10.
 */
public class ControllerHelper {
    /**
     * 获得代表查询条件的List对象
     * @param req_jsonObj 请求body对应的JSONObject对象
     * @return 代表查询条件的List对象
     */
    public static List<Condition> getConditions(JSONObject req_jsonObj) {
        JSONArray conditionList_jsonArray =
                req_jsonObj.getJSONArray("conditionList");
        //条件对象
        List<Condition> conditionList = null;
        //如果有“conditionList”键
        if (conditionList_jsonArray != null) {
            System.out.println(conditionList_jsonArray);
            //将描述条件的 JSONArray 对象转换为 Java的List 对象
            conditionList = conditionList_jsonArray.toJavaList(Condition.class);
        }
        return conditionList;
    }

    public static List<Condition> getConditions(JSONArray conditionList_jsonArray, List<Integer> removeIndex) {
        //条件对象
        List<Condition> conditionList = null;
        //如果有“conditionList”键
        if (conditionList_jsonArray != null) {
            //将描述条件的 JSONArray 对象转换为 Java的List 对象
            conditionList = conditionList_jsonArray.toJavaList(Condition.class);
            int size = removeIndex.size();
            if(size>0){
                conditionList.remove(removeIndex.get(0).intValue());
                if(size>1){
                    conditionList.remove(removeIndex.get(0).intValue());
                }
            }
        }
        return conditionList;
    }
    /**
     * 获得代表查询条件的Pagination对象
     * @param req_jsonObj 请求body对应的JSONObject对象
     * @return 代表查询条件的Pagination对象
     */
    public static Pagination getPagination(JSONObject req_jsonObj) {
        JSONObject pagination_jsonObj =
                req_jsonObj.getJSONObject("pagination");
        //分页对象
        Pagination pagination = null;
        //如果有“pagination”键
        if (pagination_jsonObj != null) {
            //将描述分页的 JSONObject 对象转换为 Java 对象
            pagination = pagination_jsonObj
                    .toJavaObject(Pagination.class);
        }
        return pagination;
    }
    public static JSONObject getMessage(Pagination pagination, String s) {
        //创建JSON对象message，以便往前端响应信息
        JSONObject message = new JSONObject();
        if(pagination!=null){
            message.put("total",pagination.getTotalNum());
        }
        message.put("data",s);
        return message;
    }

    public static int getID(JSONObject jsonObject) {
        //获取参数id的对象
        JSONObject id_object = jsonObject.getJSONObject("id");
        //如果有请求的id则查询对应id的信息，否则根据查询条件查询所有
        int id = 0;
        if(id_object!=null) {
            //获取id对象的id的值
            id = id_object.getInteger("id");
        }
        return id;
    }
    public static String getContextPath(HttpServletRequest request) throws ServletException {
        return request.getContextPath();
    }
}
